﻿using Microsoft.Xna.Framework.Graphics;

namespace WaterExample
{
    /// <summary>
    /// Initialice a new instance of a PostProcessor class.
    /// Basic class for all other PostProcessors.
    /// </summary>
    class PostProcessor
    {
        #region variables
        public Effect Effect { get; protected set; }

        public Texture2D Input { get; set; }
        public RenderTarget2D OutputRenderTarget { get; set; }
        public RenderTarget2D TempRenderTarget { get; set; }

        protected GraphicsDevice GraphicsDevice;
        protected static SpriteBatch SpriteBatch;
        public int GlobalWidth;
        public int GlobalHeight;
        #endregion

        /// <summary>
        /// Initialize a new instance of this class.
        /// Basic class for each post process.
        /// </summary>
        /// <param name="effect">Effect for rendering</param>
        /// <param name="graphicsDevice">GraphicsDevice of Game</param>
        /// <param name="globalWidth">Width of Scene</param>
        /// <param name="globalHeight">Height of Scene</param>
        public PostProcessor(Effect effect, GraphicsDevice graphicsDevice, int globalWidth, int globalHeight)
        {
            Effect = effect;
            GlobalWidth = globalWidth;
            GlobalHeight = globalHeight;
            GraphicsDevice = graphicsDevice;

            if (SpriteBatch == null)
                SpriteBatch = new SpriteBatch(graphicsDevice);
        }

        /// <summary>
        /// Virtual class to Draw the scene and catch it in a renderTarget.
        /// </summary>
        /// <param name="texture">Texture which should be drawn.</param>
        /// <returns>Returns the scene as a renderTarget.</returns>
        public virtual RenderTarget2D Draw(Texture2D texture)
        {
            if (Effect.Parameters["ScreenWidth"] != null)
                Effect.Parameters["ScreenWidth"].SetValue(GraphicsDevice.Viewport.Width);
            if (Effect.Parameters["ScreenHeight"] != null)
                Effect.Parameters["ScreenHeight"].SetValue(GraphicsDevice.Viewport.Height);

            return OutputRenderTarget;
        }
    }
}
